package com.finger.crowd.controller;

import com.finger.crowd.entity.po.MemberPO;
import com.finger.crowd.service.api.MemberService;
import com.finger.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {
    @Autowired
    private MemberService memberService;

    @RequestMapping("/get/remote/by/loginAcct")
    public ResultEntity<MemberPO> loginAcct(@RequestParam("loginAcct") String loginAcct) {
        try {
            return memberService.login(loginAcct);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResultEntity.failed("找不到您要的数据");
        }
    }

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO) {
        try {
            memberService.saveMember(memberPO);
            return ResultEntity.successWithOutData();
        } catch (Exception exception) {
            // 如果抛出的异常为用户名重复异常
            if (exception instanceof DuplicateKeyException) {
                return ResultEntity.failed("用户名已被占用");
            }
            return ResultEntity.failed(exception.getMessage());
        }

    }
}
